﻿using HslCommunication;
using HslCommunication.Profinet.Siemens;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.VisualBasic;
using MyFile.Config;
using MyFile.Init;
using MyFile.Service;
using MyFile.Utils;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Text;
using HslCommunication.ModBus;

namespace MyFile
{
    internal class Program
    {
        static Thread thread = null; //Main线程

        static bool IsExit = false; //退出线程

        public static DBUU dBUU = new DBUU();

        public static string SqlIp = ""; //数据库IP地址

        public static string path = Directory.GetCurrentDirectory(); //当前目录

        static void Main(string[] args)
        {
            string configfilepath = Path.Combine(path, "Config.json");
            if (!File.Exists(configfilepath)) File.Create(configfilepath).Close();

            RunWebApplication(args);
            SqlIp = Console.ReadLine();
            DBHelper.Instance.InitDB();
            MethodService service = new MethodService();
            Thread thread = new Thread(UU.Instance.UUGETThread);
            thread.IsBackground = true;
            thread.Start();


            //thread = new Thread(MethodThread);
            //thread.IsBackground = true;
            //thread.Start();

            Console.ReadKey();
        }
        static void MethodThread()
        {
            //SiemensInit.Instance.Init();
            //PLCFun.Instance.Init();
            //SiemensInit.Instance.Start();
            //PLCFun.Instance.Start();

            ModbusTcpNet service = new ModbusTcpNet("127.0.0.1",502,1);
            service.ConnectServer();
            while (true)
            {
                Console.WriteLine("0032\t" + service.ReadBool("0032").Content);
                Console.WriteLine("0031\t" + service.ReadBool("0031").Content);
                Console.WriteLine("0030\t" + service.ReadBool("0030").Content);

                //if (service.ReadFloat("DB6000.88.0").Content == float.Parse("1"))
                //{
                //    service.Write("DB6000.88.0", 0.0);
                //    service.Write("DB6000.0.7", false);
                //}
                //if (service.ReadFloat("DB6000.96.0").Content == float.Parse("1"))
                //{
                //    service.Write("DB6000.96.0", 0.0);
                //    service.Write("DB6000.1.1", false);
                //}
                //Console.WriteLine("DB6000.0.0\t" + service.ReadBool("DB6000.0.0").Content);
                //Console.WriteLine("DB6000.2.0\t" + service.ReadBool("DB6000.2.0").Content);
                //Console.WriteLine("DB6000.0.6\t" + service.ReadBool("DB6000.0.6").Content);
                //Console.WriteLine("DB6000.0.7\t" + service.ReadBool("DB6000.0.7").Content);
                //Console.WriteLine("DB6000.1.0\t" + service.ReadBool("DB6000.1.0").Content);
                //Console.WriteLine("DB6000.1.1\t" + service.ReadBool("DB6000.1.1").Content);
                //Console.WriteLine("DB6000.1.2\t" + service.ReadBool("DB6000.1.2").Content);
                //Console.WriteLine("DB6000.1.3\t" + service.ReadBool("DB6000.1.3").Content);
                if (IsExit)
                    break;
                Thread.Sleep(500);
                Console.Clear();
            }
        }
        static void RunWebApplication(string[] args)
        {
            Task.Factory.StartNew(() =>
            {
                var builder = WebApplication.CreateBuilder(args);
                builder.WebHost.UseUrls("http://*:21300");
                // Add services to the container.
                builder.Services.AddControllers();
                // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
                builder.Services.AddEndpointsApiExplorer();
                builder.Services.AddSwaggerGen();
                var app = builder.Build();
                app.UseSwagger();
                app.UseSwaggerUI();
                //app.UseHttpsRedirection();
                app.UseAuthorization();
                app.MapControllers();
                app.Run();
            });
        }
    }
}
